<?php

class InvoiceController extends AppController {
	
	public function create() {
		$pdf = new tFPDF();
		$pdf->AddPage();

		$number = rand(1, 99999);
		$value = number_format($number, 2, ',', ' ');
		
		$pdf->AddFont('Arial', '', 'arial.ttf', true);
		$pdf->AddFont('ArialBold', 'B', 'arialbd.ttf', true);
		$pdf->AddFont('ArialItalic', 'I', 'ariali.ttf', true);

		// zakladny hruby ram
		$pdf->SetLineWidth(0.5);
		$pdf->Cell(190, 265, '', 1);

		// horny ramik pre hlavicku
		$pdf->SetXY(10, 10);
		$pdf->SetLineWidth(0.2);
		$pdf->Cell(190, 6.5, '', 1);
		
		// navrat hore a dopisanie cisla faktury
		$pdf->SetXY(13, 10.5);
		$pdf->SetFont('ArialBold', 'B', 14);
		$pdf->SetTextColor(25, 25, 112);
		$pdf->Cell(145, 7, 'FAKTÚRA', 0);
		$pdf->Cell(40, 7, '?0012012?', 0, 0, 'R');
		
		// lavy velky ramik
		$pdf->SetXY(10, 16.5);
		$pdf->Cell(95, 82, '', 1);
		
		// navrat a vyplnenie udajov v lavom ramceku
		// najprv nazov firmy
		$pdf->SetXY(12, 18);
		$pdf->SetFont('ArialBold', 'B', 11);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Cell(50, 7, 'My New Home, s.r.o');
		
		// adresa
		$pdf->SetXY(12, 30);
		$pdf->SetFont('Arial', '', 9);
		$pdf->Cell(50, 5, 'Prostějovská 1');
		$pdf->SetXY(12, 33.5);
		$pdf->Cell(50, 5, '080 01 Prešov');
		
		// udaje pre danovy urad
		$pdf->SetXY(12, 43);
		$pdf->SetFont('Arial', '', 8);
		$pdf->Cell(50, 5, 'IČO : 46768483');
		$pdf->SetXY(12, 46.5);
		$pdf->Cell(50, 5, 'DIČ :');
		$pdf->SetXY(12, 50);
		$pdf->Cell(50, 5, 'Neplatca DPH');
		
		// label pre ucet
		$pdf->SetXY(12, 56);
		$pdf->Cell(20, 5, 'Účet');
		
		// banka
		$pdf->SetXY(12, 66);
		$pdf->Cell(20, 5, "Banka   Tatra Banka a.s.");
		
		// vratime sa a dopiseme cislo uctu vacsim pismom a boldom
		$pdf->SetXY(23, 56);
		$pdf->SetFont('ArialBold', 'B', 10);
		$pdf->Cell(20, 5, '2924879683/1100');
		
		// tri spodne ramiky
		$pdf->SetXY(10, 85.5);
		$pdf->Cell(32, 13, '', 1);
		
		$pdf->SetXY(42, 85.5);
		$pdf->Cell(31, 13, '', 1);
		
		$pdf->SetXY(73, 85.5);
		$pdf->Cell(32, 13, '', 1);
		
		// pismo na popisky
		$pdf->SetFont('Arial', '', 7);
		// lavy ramik
		$pdf->SetXY(10, 85.5);
		$pdf->Cell(32, 5, 'Dátum', 0, 0, 'C');
		$pdf->SetXY(10, 88.5);
		$pdf->Cell(32, 5, 'vyhotovenia', 0, 0, 'C');
		
		// stredny ramik
		$pdf->SetXY(42, 85.5);
		$pdf->Cell(32, 5, 'Dátum dodania', 0, 0, 'C');
		$pdf->SetXY(42, 88.5);
		$pdf->Cell(32, 5, 'tovaru / služby', 0, 0, 'C');
		
		// pravy ramik
		$pdf->SetXY(73, 85.5);
		$pdf->Cell(32, 5, 'Splatnosť', 0, 0, 'C');
		$pdf->SetXY(73, 88.5);
		$pdf->Cell(32, 5, 'najneskôr do', 0, 0, 'C');
		
		// pismo na datumy
		$pdf->SetFont('ArialBold', 'B', 11);
		// este do ramcekov doplnime datumy
		$pdf->SetXY(10, 93);
		$pdf->Cell(32, 5, '?16.8.2012?', 0, 0, 'C');
		
		$pdf->SetXY(42, 93);
		$pdf->SetTextColor(0, 0, 255);
		$pdf->Cell(32, 5, '?16.8.2012?', 0, 0, 'C');
		
		$pdf->SetXY(73, 93);
		$pdf->SetTextColor(255, 0, 0);
		$pdf->Cell(32, 5, '?23.8.2012?', 0, 0, 'C');
		
		// vlozime obrazok
		$pdf->Image('images/mnhlogo.png', 67, 32, 28);

		// pravy velky ramik
		$pdf->SetXY(105, 16.5);
		$pdf->Cell(95, 82, '', 1);
		
		// navrat a vyplnenie udajov v pravom ramceku
		$pdf->SetXY(105, 16.5);
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Cell(95, 6, ' Objednávka :', 1);
		
		// vytvorenie laveho ramceka pre sposob uhrady
		$pdf->SetXY(105, 22.5);
		$pdf->Cell(48, 9, '', 1);
		
		$pdf->SetXY(106, 23);
		$pdf->Cell(24, 5, 'Spôsob úhrady :');
		
		$pdf->SetXY(125, 26);
		$pdf->SetFont('ArialBold', 'B', 8);
		$pdf->Cell(24, 5, '?Prevodný príkaz?');
		
		// vytvorenie praveho ramceka pre symboly
		$pdf->SetXY(153, 22.5);
		$pdf->Cell(47, 9, '', 1);
		
		// popisky k symbolom
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetXY(155, 23);
		$pdf->Cell(24, 5, 'Variabilný symbol   :');
		
		$pdf->SetXY(155, 26);
		$pdf->Cell(24, 5, 'Konštantný symbol :');
		
		// hodnoty k symbolom
		$pdf->SetFont('ArialBold', 'B', 7);
		$pdf->SetXY(179, 23);
		$pdf->Cell(19, 5, '?0012012?', 0, 0, 'R');
		
		$pdf->SetXY(179, 26);
		$pdf->Cell(19, 5, '?0308?', 0, 0, 'R');
		
		// korenspondencna adresa
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetXY(106, 32);
		$pdf->Cell(24, 5, 'Korešpondenčná adresa :');
		
		$pdf->SetFont('Arial', '', 11);
		$pdf->SetXY(109, 50);
		$pdf->Cell(24, 5, '?LEONIDES REAL PLUS, s.r.o.?');
		
		$pdf->SetXY(109, 60);
		$pdf->Cell(24, 5, '?Levočská 5?');
		
		$pdf->SetXY(109, 65);
		$pdf->Cell(24, 5, '?080 01 Prešov?');
		
		$pdf->SetXY(109, 70);
		$pdf->Cell(24, 5, '?SR?');
		
		// ciara
		$pdf->SetXY(110, 80);
		$pdf->Cell(85, 0, '', 1);
		
		// odberatel
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetXY(109, 80.5);
		$pdf->Cell(24, 5, 'Odberateľ :');
		
		$pdf->SetXY(109, 86);
		$pdf->Cell(24, 5, 'IČO : 46556133');
		
		$pdf->SetXY(109, 89);
		$pdf->Cell(24, 5, 'DIČ : 2023447492');
		
		$pdf->SetXY(109, 92);
		$pdf->Cell(24, 5, 'IČ DPH :');
		
		$pdf->SetFont('Arial', '', 8);
		$pdf->SetXY(142, 80.5);
		$pdf->Cell(24, 5, '?LEONIDES REAL PLUS, s.r.o.?');
		
		$pdf->SetXY(142, 86);
		$pdf->Cell(24, 5, '?Levočská 5?');
		
		$pdf->SetXY(142, 89.5);
		$pdf->Cell(24, 5, '?080 01 Prešov?');
		
		$pdf->SetXY(142, 93);
		$pdf->Cell(24, 5, '?SR?');
		
		// sumar
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetFillColor(221, 221, 221);
		
		// hlavicka sumaru
		$pdf->SetXY(10.5, 108);
		$pdf->Cell(114, 5, 'Názov / Popis', 1, 0, 'C', TRUE);
		$pdf->Cell(20, 5, 'Množstvo', 1, 0, 'C', TRUE);
		$pdf->Cell(10, 5, 'MJ', 1, 0, 'C', TRUE);
		$pdf->Cell(20, 5, 'Cena za MJ', 1, 0, 'C', TRUE);
		$pdf->Cell(25, 5, 'Celková cena', 1, 0, 'C', TRUE);
		
		// hodnoty sumaru
		$pdf->SetXY(10.5, 113);
		$pdf->Cell(114, 8, '?Fakturujeme Vám za sprostredkovanie obchodu a služieb, klienti Baranek - Semeník?', 1);
		$pdf->Cell(20, 8, '?1?', 1, 0, 'R');
		$pdf->Cell(10, 8, '?', 1, 0, 'C');
		$pdf->Cell(20, 8, '?' . $value . '?', 1, 0, 'R');
		$pdf->Cell(25, 8, '?' . $value . '?', 1, 0, 'R');
		
		// sucet
		$pdf->SetXY(10.5, 121);
		$pdf->Cell(164, 5, 'Celková suma >>>', 1, 0, 'R', TRUE);
		$pdf->Cell(25, 5, '?' . $value . '?', 1, 0, 'R', TRUE);
		
		// spodna cast
		$pdf->SetXY(10.5, 235);
		// ramceky
		$pdf->Cell(69, 35, '', 1);
		$pdf->Cell(60, 35, '', 1);
		$pdf->Cell(60, 35, '', 1);
		
		$pdf->SetFont('Arial', '', 8);
		// popisok vlavo
		$pdf->setXY(12, 236);
		$pdf->Cell(69, 5, 'Vyhotovil :');
		
		// popisok v strede
		$pdf->setXY(81, 236);
		$pdf->Cell(60, 5, 'Prevzal :');
		
		// hodnota vlavo
		$pdf->SetFont('ArialBold', 'B', 6);
		$pdf->setXY(12, 263);
		$pdf->Cell(60, 5, '?Administrátor?');
		
		// tabulka v pravom ramceku
		$pdf->SetFillColor(240, 240, 240);
		$pdf->SetXY(139.5, 235);
		$pdf->SetFont('Arial', '', 7);
		$pdf->Cell(31, 5, ' Celková suma', 1, 0, 'L', TRUE);
		$pdf->SetFont('ArialBold', 'B', 7);
		$pdf->SetTextColor(0, 0, 255);
		$pdf->Cell(29, 5, '?' . $value . '?', 1, 1, 'R');
		$pdf->SetXY(139.5, 240);
		$pdf->SetFont('Arial', '', 7);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Cell(31, 5, ' Uhradené zálohami', 1, 0, 'L', TRUE);
		$pdf->SetFont('ArialBold', 'B', 7);
		$pdf->Cell(29, 5, '?0,00?', 1, 1, 'R');
		$pdf->SetXY(139.5, 245);
		$pdf->SetFont('Arial', '', 7);
		$pdf->Cell(31, 5, ' Zostáva uhradiť', 1, 0, 'L', TRUE);
		$pdf->SetFont('ArialBold', 'B', 7);
		$pdf->SetTextColor(255, 0, 0);
		$pdf->Cell(29, 5, '?' . $value . '?', 1, 1, 'R');
		
		// a este raz suma na zaver
		$pdf->SetXY(139.5, 251);
		$pdf->SetFont('Arial', '', 9);
		$pdf->Cell(31, 5, 'K úhrade :');
		$pdf->SetFont('Arial', '', 6);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Cell(29, 5, '(zaokrúhlenie 0,00 EUR)');
		
		// velka suma
		$pdf->SetXY(139.5, 256);
		$pdf->SetFont('Arial', '', 15);
		$pdf->SetTextColor(255, 0, 0);
		$pdf->Cell(58, 5, '?' . $value . '? EUR', 0, 0, 'R');
		$pdf->SetXY(139.5, 260);
		$pdf->SetFont('ArialItalic', 'I', 6);
		$pdf->Cell(58, 5, '?' . ucfirst($this->getTextForNumber($number)) . '?', 0, 0, 'R');
		$pdf->SetXY(139.5, 265);
		$pdf->SetFont('Arial', '', 5);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Cell(35, 5, 'Konverzný kurz : 30,1260 Sk/EUR');
		
		$pdf->SetXY(174.5, 265);
		$pdf->SetFont('Arial', '', 9);
		$pdf->SetTextColor(255, 0, 0);
		$pdf->Cell(23, 5, '?' . number_format($number * 30.126, 2, ',', ' ') . '? Sk', 0, 0, 'R');
		
		// ulozime pdf do suboru
		$pdf->Output('test.pdf', 'F');
		exit();
	}
	
	protected function getTextForNumber($number) {
		$text = '';
		
		$number = str_replace('.', ',', $number);
		$numberParts = explode(',', $number);
		$euros = 0;
		if (isset($numberParts[0])) {
			$euros = $numberParts[0];
		}
		
		$text .= $this->getTextForEuros($euros);
		
		$cents = 0;
		if (isset($numberParts[1])) {
			$cents = $numberParts[1];
		}
		
		$text .= $this->getTextForCents($cents);
		
		return $text;
	}
	
	protected function getTextForEuros($euros) {
		$text = '';
		
		if ($euros > 100) {
			$nonHundreds = $euros % 100;
			$hundreds = ($euros - $nonHundreds) / 100;
			if ($hundreds < 10) {
				$text .= $this->getBasicWord($hundreds, 'middle') . 'sto';
			} else {
				$realHundreds = $hundreds % 10;
				$thousands = ($hundreds - $realHundreds) / 10;
				if ($thousands == 1) {
					$text .= $this->getBasicWord($thousands, 'male');
				} elseif ($thousands < 20) {
					$text .= $this->getBasicWord($thousands, 'female');
				} elseif ($thousands < 50) {
					$text .= $this->get20To50Word($thousands, 'male');
				} else {
					$text .= $this->get50MoreWord($thousands, 'male');
				} 
				$text .= 'tisíc';
				// ani tu nechceme mat nulasto
				if ($realHundreds != 0) {
					$text .= $this->getBasicWord($realHundreds, 'middle') . 'sto';
				}
			}
		} else {
			$nonHundreds = $euros;
		}
		
		if ($nonHundreds < 20) {
			$text .= $this->getBasicWord($nonHundreds, 'male');
		} elseif ($nonHundreds < 50) {
			$text .= $this->get20To50Word($nonHundreds, 'male');
		} else {
			$text .= $this->get50MoreWord($nonHundreds, 'male');
		}
		
		$text .= ' EUR';
		return $text;
	}
	
	protected function getTextForCents($cents) {
		if ($cents == 0) {
			return '';
		} else {
			return $cents;
		}
	}
	
	protected function getBasicWord($number, $gender) {
		switch ($number) {
			case 0: return 'nula';
			case 1:
				if ($gender == 'male') {
					return 'jeden';
				} else {
					return 'jedno';
				}
			case 2:
				if ($gender == 'male') {
					return 'dva';
				} else {
					return 'dve';
				}
			case 3: return 'tri';
			case 4: return 'štyri';
			case 5: return 'pať';
			case 6: return 'šesť';
			case 7: return 'sedem';
			case 8: return 'osem';
			case 9: return 'deväť';
			case 10: return 'desať';
			case 11: return 'jedenásť';
			case 12: return 'dvanásť';
			case 13: return 'trinásť';
			case 14: return 'štrnásť';
			case 15: return 'pätnásť';
			case 16: return 'šestnásť';
			case 17: return 'sedemnásť';
			case 18: return 'osemnásť';
			case 19: return 'devätnásť';
		}
	}
	
	protected function get20To50Word($number, $gender) {
		$ones = $number % 10;
		$tens = ($number - $ones) / 10;
		
		$text = '';
		$text .= $this->getBasicWord($tens, $gender);
		$text .= 'dsať';
		// nechceme aby sa nam vypisalo dvadsatnula :)
		if ($ones != 0) {
			$text .= $this->getBasicWord($ones, $gender);
		}
		return $text;
	}
	
	protected function get50MoreWord($number, $gender) {
		$ones = $number % 10;
		$tens = ($number - $ones) / 10;
		
		$text = '';
		$text .= $this->getBasicWord($tens, $gender);
		$text .= 'desiat';
		// nechceme aby sa nam vypisalo patdesiatnula :)
		if ($ones != 0) {
			$text .= $this->getBasicWord($ones, $gender);
		}
		return $text;
	}
}

?>